package gwt.bbsbot.client.widget;

import gwt.bbsbot.client.GreetingService;
import gwt.bbsbot.client.GreetingServiceAsync;
import gwt.bbsbot.client.constatns.Constants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public abstract class BbsWidget extends VerticalPanel {
	
	protected static String MyGenre;
	
	protected DebugPanel debug = new DebugPanel();
	protected Label lbTitle = new Label("def");
	
	//外枠
	protected VerticalPanel panelBbs = new VerticalPanel();
	//BBSの内容を表示するFlexTableを持ったパネル
	protected VerticalPanel panelFxBbs = new VerticalPanel();
	//BBSの内容を表示する
	protected FlexTable fxTable = new FlexTable();
	//新規投稿用入力フォーム
	protected DialogBox dlgInputForm = new DialogBox(true);
	protected Button btNew = new Button("新規");
	protected TextBox nameInDlg = new TextBox();
	protected TextArea contentInDlg = new TextArea();
	

	protected final GreetingServiceAsync greetingService = GWT
	.create(GreetingService.class);

	/**
	 * タイトルラベルを設定する
	 */
	public void setTitle(String title) {
		this.lbTitle.setText(title);
	}
	
	/**
	 * コンストラクタ
	 */
	public BbsWidget() {
		init();
	}

	protected void init(){
		this.panelBbs.add(this.debug);
		this.panelBbs.add(lbTitle);
		this.panelFxBbs.add(this.fxTable);
		this.panelBbs.add(this.panelFxBbs);
		this.panelBbs.add(this.btNew);
		
		this.btNew.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				dlgInputForm.center();
			}
		});
		createInputForm();
		this.add(this.panelBbs);
	}
	
	
	/**
	 * 入力ダイアログの作成
	 */
	protected void createInputForm(){
		//入力フォーム
		FlexTable fxpanel = new FlexTable();
		this.nameInDlg.setText("七誌");
		this.contentInDlg.setSize("200px", "100px");
		HorizontalPanel panelButton = new HorizontalPanel();
		Button btCancel = new Button("cancel");
		Button btSend = new Button("Send");
		panelButton.add(btCancel);
		panelButton.add(btSend);
		
		//panel設定
		fxpanel.setText( 0, 0, "Name");
		fxpanel.setText( 1, 0, "内容");
		fxpanel.setWidget(0, 1, nameInDlg);
		fxpanel.setWidget(1, 1, contentInDlg);
		fxpanel.setWidget(2, 1, panelButton);
		
		this.dlgInputForm.setText("新規投稿");
		this.dlgInputForm.setWidget(fxpanel);
		this.dlgInputForm.setAnimationEnabled(true);
		//キャンセルボタンのイベント
		btCancel.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				//dialogを隠す
				dlgInputForm.hide();		
			}
			
		});

		createAddHandler(btSend, nameInDlg.getText(), contentInDlg.getText());
	}
	
	protected abstract void createAddHandler(Button button, String name, String context);	
	public abstract void displayBBS();

}
